rm(list=ls())
data<-read.csv(file.choose())
library(glmnet)
data$category <- as.factor(data$category)
data$education <- as.factor(data$education)
data$district <- as.factor(data$district)
data$sleep <- as.factor(data$sleep)
data$BMI <- as.factor(data$BMI)
data$satisfied <- as.factor(data$satisfied)
data$depression <- as.factor(data$depression)
data$family.size <- as.factor(data$family.size)
data$activities.of.daily.living<- as.factor(data$activities.of.daily.livin)
data$X.classification..Number.of.chronic.diseases<- as.factor(data$X.classification..Number.of.chronic.diseases)
x <- model.matrix(category ~ ., data = data)[, -1]
y <- data$category
fit <- glmnet(x, y, family = "multinomial", alpha = 1)
plot(fit)
print(fit)
cvfit <- cv.glmnet(x, y, family = "multinomial", alpha = 1, type.measure = "class")
plot(cvfit)
print(cvfit$lambda.min)
cvfit$lambda.min
optimal_lambda <- cvfit$lambda.min
final_model <- glmnet(x, y, family = "multinomial", alpha = 1, lambda = optimal_lambda)
coef(final_model)
coef_1 <- as.matrix(coef(final_model)$"1")
coef_2 <- as.matrix(coef(final_model)$"2")
coef_3 <- as.matrix(coef(final_model)$"3")
write.csv(coef_1,file="~/coef1.csv")
write.csv(coef_2,file="~/coef2.csv")
write.csv(coef_3,file="~/coef3.csv")




